package com.obas.monitor;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.obas.business.common.vo.AtMessage;
import com.obas.monitor.manager.FeedManager;
import com.obas.monitor.manager.SpringContextUtil;

import flex.messaging.io.amf.ASObject;

public class Recorder {
   public static Recorder recorder = null;
   public static String filePath = "C:\\work\\work-itest\\work-java\\com.obas.runtime.java/src/com/pktech/itest/monitor/";
   public static String fileName = "record.txt";
   public static String CONSTLITERAL = "___constliteral___";

   @SuppressWarnings("unchecked")
   public static void recode(AtMessage msg) {
      if (recorder == null) {
         recorder = new Recorder();
      }

      ASObject p = (ASObject)(msg.getParam());
      StringBuffer result = new StringBuffer("<" + msg.getId() + ", " + msg.getTime() + ", ");
      ASObject locator = (ASObject)(p.get("locator"));
      if (locator != null) {
         Object[] parts = (Object[])locator.get("parts");
         String path = "";
         for (int i = 0; i < parts.length; i++) {
            Map<String, String> part = (Map<String, String>)parts[i];
            path += part.get("automationClassName") + "(" + part.get("automationName") + ")->";
         }
         result.append(path.substring(0, path.length() - 2));
      }
      String method = (String)p.get("method");
      result.append(", " + method + "(");
      if (method != null) {
         Object[] args = (Object[])(p.get("args"));
         if (args != null && args.length > 0) {
            String value = "";
            for (int i = 0; i < args.length; i++) {
               Object arg = args[i];
               if (arg instanceof ASObject) {
                  value += ((ASObject)arg).get(CONSTLITERAL) + ",";
               }
               else {
                  value += arg.toString() + ",";
               }
            }
            result.append(value.substring(0, value.length() - 1) + ")");
         }
         else {
            result.append(")");
         }
      }
      result.append(">\n\n");
      FeedManager feedManager = (FeedManager)SpringContextUtil.getBean("feedManager");
      String appid = ((Map<String, Object>)(msg.getParam())).get("pageId") + "";
      Map<String, Object> param = new HashMap<String, Object>();
      param.put("recordingText", result.toString());
      AtMessage message = new AtMessage(1000, msg.getCommand(), param);
      feedManager.sendMsg(message, appid);
      recorder.writeText(fileName, result.toString());
   }

   public boolean writeText(String textname, String data) {
      boolean flag=false;
      File file = new File(filePath);
      if(!file.exists()){
         file.mkdirs();
      }
      try {
          FileWriter fw =new FileWriter(filePath+File.separator+textname, true);
          fw.append(data);
          flag=true;
          if (fw!=null)
              fw.close();
      } catch (IOException e) {
          e.printStackTrace();
      }

      return flag;
  }
}
